package com.opc.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.opc.dto.DeviceRecordTimesDto;
import com.opc.dto.HistoryRecordDto;
import com.opc.entity.DeviceRecord;
import com.opc.entity.PageRes;
import com.opc.excel.HistoryExcel;
import com.opc.mapper.DeviceRecordMapper;
import com.opc.service.DeviceRecordService;
import com.opc.utils.RedisUtils;
import com.opc.utils.StringUtils;
import org.ehcache.impl.internal.concurrent.ConcurrentHashMap;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 作者
 * @since 2023-03-14
 */
@Service
public class DeviceRecordServiceImpl extends ServiceImpl<DeviceRecordMapper, DeviceRecord> implements DeviceRecordService {

    @Resource
    private DeviceRecordMapper deviceRecordMapper;

    @Resource
    RedisUtils redisUtils;


    @Override
    public DeviceRecord selectLatestRecordByDcAndType(String deviceCode, Integer type) {
        return deviceRecordMapper.selectLatestRecordByDcAndType(deviceCode, type);
    }

    @Override
    public int updateDeviceRecordByCodesAndType(List<String> uIdList, int type) {
        Map<String, Object> paramMap = new ConcurrentHashMap<>();
        paramMap.put("uIdList", uIdList);
        paramMap.put("type", type);
        return deviceRecordMapper.updateDeviceRecordByCodesAndType(paramMap);
    }

    @Override
    public List<DeviceRecord> selectAllRecordListByIds(List<String> dList, Date searchTime) {
        Map<String, Object> paramMap = new ConcurrentHashMap<>();
        paramMap.put("dList", dList);
        if (searchTime != null) {
            paramMap.put("searchTime", searchTime);
        }
        return deviceRecordMapper.selectAllRecordByIds(paramMap);
    }

    @Override
    public int updateDeviceRecordByIds(List<Long> uIdList) {
        Map<String, Object> paramMap = new ConcurrentHashMap<>();
        paramMap.put("uIdList", uIdList);
        return deviceRecordMapper.updateDeviceRecordByIds(paramMap);
    }

    @Override
    public List<DeviceRecord> selectConsumeByProLine(Date startTime, Date endTime, String proLine) {
        Map<String, Object> paramMap = new ConcurrentHashMap<>();
        paramMap.put("startTime", startTime);
        paramMap.put("endTime", endTime);
        paramMap.put("proLine", proLine);
        return deviceRecordMapper.selectConsumeByProLine(paramMap);
    }

    @Override
    public List<DeviceRecordTimesDto> selectConsumeByProLineNew(Date startTime, Date endTime, Integer offset) {
        Map<String, Object> paramMap = new ConcurrentHashMap<>();
        paramMap.put("startTime", startTime);
        paramMap.put("endTime", endTime);
        paramMap.put("offset", offset);
        return deviceRecordMapper.selectConsumeByProLineNew(paramMap);
    }

    @Override
    public PageRes<HistoryRecordDto> getAbnormalHistory(String proLine, String startTime, String endTime, String valType, String recordType, Integer pageNo, Integer pageSize) {
        Map<String, Object> paramMap = new ConcurrentHashMap<>();
        Page<HistoryRecordDto> page = new Page<>(pageNo, pageSize);
        if (!StringUtils.isBlank(recordType)) {
            paramMap.put("recordType", recordType);
        }
        if (!StringUtils.isBlank(startTime)) {
            Date start = DateUtil.parse(startTime, "yyyy-MM-dd HH:mm:ss");
            paramMap.put("startTime", start);
        }
        if (!StringUtils.isBlank(endTime)) {
            Date end = DateUtil.parse(endTime, "yyyy-MM-dd HH:mm:ss");
            paramMap.put("endTime", end);
        }
        if (!StringUtils.isBlank(proLine)) {
            paramMap.put("proLine", proLine);
        }
        paramMap.put("valType", valType);
        IPage<HistoryRecordDto> records = deviceRecordMapper.getAbnormalHistory(page, paramMap);
        if (records.getRecords() != null && !records.getRecords().isEmpty()) {
            for (HistoryRecordDto record : records.getRecords()) {
                Object val = redisUtils.hmGet(record.getSetKey(), "val");
                if (val != null) {
                    record.setSetVal(Long.parseLong(val + ""));
                }
            }
        }
        return new PageRes<>(pageNo, pageSize, records.getTotal(), records.getRecords());
    }

    @Override
    public PageRes<HistoryRecordDto> getHistoryList(String proLine, String startTime, String endTime, String valType, String recordType, Integer pageNo, Integer pageSize) {
        Map<String, Object> paramMap = new ConcurrentHashMap<>();
        Page<HistoryRecordDto> page = new Page<>(pageNo, pageSize);
        if (!StringUtils.isBlank(recordType)) {
            paramMap.put("recordType", recordType);
        }
        if (!StringUtils.isBlank(startTime)) {
            Date start = DateUtil.parse(startTime, "yyyy-MM-dd HH:mm:ss");
            paramMap.put("startTime", start);
        }
        if (!StringUtils.isBlank(endTime)) {
            Date end = DateUtil.parse(endTime, "yyyy-MM-dd HH:mm:ss");
            paramMap.put("endTime", end);
        }
        if (!StringUtils.isBlank(proLine)) {
            paramMap.put("proLine", proLine);
        }
        paramMap.put("valType", valType);
        IPage<HistoryRecordDto> records = deviceRecordMapper.getHistoryList(page, paramMap);
        return new PageRes<>(pageNo, pageSize, records.getTotal(), records.getRecords());
    }

    @Override
    public List<HistoryExcel> getHistoryListExcel(String proLine, String startTime, String endTime, String valType, String recordType) {
        Map<String, Object> paramMap = new ConcurrentHashMap<>();
        if (!StringUtils.isBlank(recordType)) {
            paramMap.put("recordType", recordType);
        }
        if (!StringUtils.isBlank(startTime)) {
            Date start = DateUtil.parse(startTime, "yyyy-MM-dd HH:mm:ss");
            paramMap.put("startTime", start);
        }
        if (!StringUtils.isBlank(endTime)) {
            Date end = DateUtil.parse(endTime, "yyyy-MM-dd HH:mm:ss");
            paramMap.put("endTime", end);
        }
        if (!StringUtils.isBlank(proLine)) {
            paramMap.put("proLine", proLine);
        }
        paramMap.put("valType", valType);
        return deviceRecordMapper.getHistoryListExcel(paramMap);

    }

    @Override
    public List<HistoryRecordDto> getAbnormalHistoryExcel(String proLine, String startTime, String endTime, String valType, String recordType) {
        Map<String, Object> paramMap = new ConcurrentHashMap<>();
        if (!StringUtils.isBlank(recordType)) {
            paramMap.put("recordType", recordType);
        }
        if (!StringUtils.isBlank(startTime)) {
            Date start = DateUtil.parse(startTime, "yyyy-MM-dd HH:mm:ss");
            paramMap.put("startTime", start);
        }
        if (!StringUtils.isBlank(endTime)) {
            Date end = DateUtil.parse(endTime, "yyyy-MM-dd HH:mm:ss");
            paramMap.put("endTime", end);
        }
        if (!StringUtils.isBlank(proLine)) {
            paramMap.put("proLine", proLine);
        }
        paramMap.put("valType", valType);
        List<HistoryRecordDto> records = deviceRecordMapper.getAbnormalHistoryExcel(paramMap);
        if (records != null && !records.isEmpty()) {
            for (HistoryRecordDto record : records) {
                Object val = redisUtils.hmGet(record.getSetKey(), "val");
                if (val != null) {
                    record.setSetVal(Long.parseLong(val + ""));
                }
            }
        }
        return records;
    }

    @Override
    public int delAbnormal(Long[] recordIds) {
        Map<String, Object> paramMap = new ConcurrentHashMap<>();
        paramMap.put("recordIds", recordIds);
        return deviceRecordMapper.delAbnormal(paramMap);
    }
}




















